package V003_MapLoader;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.LinkedList;

/**
 *
 * @author Smai
 *
 * Kartta tiedosto jota luetaan pitää olla muotoa
 *
 * posX posY width height type info collision
 */
public class MapLoader {

  public static Tile[][] CreateMap(int mapWidth, int mapHeight, String mapName) {
    //Alustetaan worldTiles taulukko
    Tile[][] worldTiles = new Tile[mapHeight][mapWidth];

    int arrayX = 0;
    int arrayY = 0;

    int posX;
    int posY;
    int width;
    int height;
    String type;
    String info;
    int collisionBoolean;
    boolean collision;


    try {
      System.out.println("Loading map...");

      BufferedReader br = new BufferedReader(new FileReader(new File("Resources//Maps//" + mapName + ".txt")));
      String luettuRivi = br.readLine();
      while (luettuRivi != null) {

        String[] pilkottuRivi = luettuRivi.split(" ");

        posX = Integer.parseInt(pilkottuRivi[0]);
        posY = Integer.parseInt(pilkottuRivi[1]);
        width = Integer.parseInt(pilkottuRivi[2]);
        height = Integer.parseInt(pilkottuRivi[3]);
        type = pilkottuRivi[4];
        info = pilkottuRivi[5];
        collisionBoolean = Integer.parseInt(pilkottuRivi[6]);

        if (collisionBoolean == 1) {
          collision = true;
        } else {
          collision = false;
        }

        worldTiles[arrayY][arrayX] = new Tile(posX, posY, width, height, type, info, collision);

        if (arrayX == mapWidth - 1 && arrayY == mapHeight - 1) {
          System.out.println("Map Ready!");
          return worldTiles;
        } else if (arrayX == mapWidth - 1) {
          arrayX = 0;
          arrayY++;
        } else {
          arrayX++;
        }


        luettuRivi = br.readLine();

      }
    } catch (FileNotFoundException ex) {
      ex.printStackTrace();
      return null;
    } catch (IOException ex) {
      ex.printStackTrace();
      return null;
    }

    System.out.println("Map Ready!");
    return worldTiles;
  }
}
//
//            worldTiles[i][j] = new Tile(i,j,32,32,"grave","Grave stone",true);

